#ifndef __NET_H__
#define __NET_H__

#include "global.h"

#define BUFFER_SIZE 20000

#define ETHER_ADDR_LEN 6

#define NETIF_HEAD_MAX_LEN 14

#define ETHER_TYPE_IP	0x0800	
#define ETHER_TYPE_ARP  0x0806
#define ETHER_TYPE_RARP 0x8035
#define ETHER_TYPE_SNMP 0x814C

struct SEtherHead		  //14
{
	uchar	DestAddr[ETHER_ADDR_LEN];
	uchar	ScrAddr[ETHER_ADDR_LEN];

	/* 16 bits.0800H IP, 0806H ARP, value less than 0x0600 used in 
	IEEE802 to indicate the length of the packet*/
	uint type;	
	
};

struct SARPPacket
{
	// header 
	uint HardwareType;
	uint ProtocolType;
	uchar HardWareAddrLen;
	uchar ProtocolAddrLen;
	uint type;				// refer to arp type 

	// arp content 
	uchar	EtherScrAddr[ETHER_ADDR_LEN];
	ulong		IPScrAddr;
	uchar	EtherDestAddr[ETHER_ADDR_LEN];
	ulong		IPDestAddr;
};

#define IP_VERSION_4	4

#define IP_HEAD_MIN_LEN	20	/* ip head len with no option */

#define IP_INITIAL_LIFE 64 

#define IP_PROTOCOL_TCP		0x06	
#define IP_PROTOCOL_ICMP	0x01
#define IP_PROTOCOL_UDP 	0x11

#define IP_VERSION(pIPHead) (pIPHead->Ver_HeadLen>>4)
#define IP_HEAD_LEN(pIPHead) ((pIPHead->Ver_HeadLen & 0x0f)*4)

#define	IP_FRAGMENT_OFFSET_MASK	0x1FFF	\

struct SIPPacket			  //20
{
	uchar Ver_HeadLen;			 /* Version 4 bits, HeadLength 4 bits. typical value is 0x45 */
	uchar ServeType;	
	uint TotalLen;				/* all size of IP packet inlcude IPHead. 16 bits */
	uint FragmentID;			/* 16 bits */
	uint FragmentFlag_Offset;
	uchar LifeLength;			/* ttl */
	uchar Protocol;				/* eg. IP_PROTOCAL_TCP*/
	uint CheckSum;				/* 16 bits */
	ulong IPScr;				/* 32 bits */
	ulong IPDest;
};

struct STCPPacket
{
	uint PortScr;	/* 16 bits */
	uint PortDest;
	ulong Seq;
	ulong AckSeq;
	uchar TCPHeadLen;	/* 8 bits, use high 4 bits */
	uchar flag;			/* reserve 2 bits, urg, ack, psh, rst, syn, fin */
	uint WndSize;		/* 16 bits */
	uint CheckSum;		/* 16 bits */
	uint UrgentPoint;	/* 16 bits */
};

#define ICMP_TYPE_QUERY	8
#define ICMP_TYPE_REPLY	0

struct SICMPPacket	//8
{
	uchar type;
	uchar ICMPCode;
	uint CheckSum;
	uint id;
	uint seq;
};

struct SUDPPacket			  //8
{
	uint	PortScr;
	uint	PortDest;
	uint    TotalLen;
	uint    CheckSum;
};
// ******* ETH *******
#define ETH_HEADER_LEN	14
// values of certain bytes:
#define ETHTYPE_ARP_H_V 0x08
#define ETHTYPE_ARP_L_V 0x06
#define ETHTYPE_IP_H_V  0x08
#define ETHTYPE_IP_L_V  0x00
// byte positions in the ethernet frame:
//
// Ethernet type field (2bytes):
#define ETH_TYPE_H_P 12
#define ETH_TYPE_L_P 13
//
#define ETH_DST_MAC 0
#define ETH_SRC_MAC 6


// ******* ARP *******
#define ETH_ARP_OPCODE_REPLY_H_V 0x0
#define ETH_ARP_OPCODE_REPLY_L_V 0x02
//
#define ETHTYPE_ARP_L_V 0x06
// arp.dst.ip
#define ETH_ARP_DST_IP_P 0x26
// arp.opcode
#define ETH_ARP_OPCODE_H_P 0x14
#define ETH_ARP_OPCODE_L_P 0x15
// arp.src.mac
#define ETH_ARP_SRC_MAC_P 0x16
#define ETH_ARP_SRC_IP_P 0x1c
#define ETH_ARP_DST_MAC_P 0x20
#define ETH_ARP_DST_IP_P 0x26

// ******* IP *******
#define IP_HEADER_LEN	20
// ip.src
#define IP_SRC_P 0x1a
#define IP_DST_P 0x1e
#define IP_HEADER_LEN_VER_P 0xe
#define IP_CHECKSUM_P 0x18
#define IP_TTL_P 0x16
#define IP_FLAGS_P 0x14
#define IP_P 0xe
#define IP_TOTLEN_H_P 0x10
#define IP_TOTLEN_L_P 0x11

#define IP_PROTO_P 0x17  

#define IP_PROTO_ICMP_V 1
#define IP_PROTO_TCP_V 6
// 17=0x11
#define IP_PROTO_UDP_V 17
// ******* ICMP *******
#define ICMP_TYPE_ECHOREPLY_V 0
#define ICMP_TYPE_ECHOREQUEST_V 8
//
#define ICMP_TYPE_P 0x22
#define ICMP_CHECKSUM_P 0x24

// ******* UDP *******
#define UDP_HEADER_LEN	8
//
#define UDP_SRC_PORT_H_P 0x22
#define UDP_SRC_PORT_L_P 0x23
#define UDP_DST_PORT_H_P 0x24
#define UDP_DST_PORT_L_P 0x25
//
#define UDP_LEN_H_P 0x26
#define UDP_LEN_L_P 0x27
#define UDP_CHECKSUM_H_P 0x28
#define UDP_CHECKSUM_L_P 0x29
#define UDP_DATA_P 0x2a

// ******* TCP *******
#define TCP_SRC_PORT_H_P 0x22
#define TCP_SRC_PORT_L_P 0x23
#define TCP_DST_PORT_H_P 0x24
#define TCP_DST_PORT_L_P 0x25
// the tcp seq number is 4 bytes 0x26-0x29
#define TCP_SEQ_H_P 0x26
#define TCP_SEQACK_H_P 0x2a
// flags: SYN=2
#define TCP_FLAGS_P 0x2f
#define TCP_FLAGS_SYN_V 2
#define TCP_FLAGS_FIN_V 1
#define TCP_FLAGS_PUSH_V 8
#define TCP_FLAGS_SYNACK_V 0x12
#define TCP_FLAGS_ACK_V 0x10
#define TCP_FLAGS_PSHACK_V 0x18
//  plain len without the options:
#define TCP_HEADER_LEN_PLAIN 20
#define TCP_HEADER_LEN_P 0x2e
#define TCP_CHECKSUM_H_P 0x32
#define TCP_CHECKSUM_L_P 0x33
#define TCP_OPTIONS_P 0x36
//
#endif
//@}

